all files / tooltip/ tooltip.vue

5.56% Statements 2/36
0% Branches 0/28
0% Functions 0/8
3.7% Lines 1/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
//
//
//
//
//
//
//
//
 
export default {
  name: 'mu-tooltip',
  props: {
    label: {
      type: String
    },
    trigger: {
      type: Element
    },
    verticalPosition: {
      type: String, // top bottom
      default: 'bottom'
    },
    horizontalPosition: {
      type: String, // left right center
      default: 'center'
    },
    show: {
      type: Boolean,
      default: false
    },
    touch: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      offsetWidth: 0,
      triggerWidth: 0,
      triggerHeight: 0
    }
  },
  computed: {
    tooltipStyle () {
      const {horizontalPosition, verticalPosition, offsetWidth, touch, triggerWidth, triggerHeight, show} = this
      const touchMarginOffset = touch ? 10 : 0
      const touchOffsetTop = touch ? -20 : -10
      const offset = verticalPosition === 'bottom' ? 14 + touchMarginOffset : -14 - touchMarginOffset
      return {
        right: horizontalPosition === 'left' ? '0' : null,
        left: horizontalPosition === 'center' ? ((offsetWidth - triggerWidth) / 2 * -1) + 'px' : horizontalPosition === 'right' ? '0' : '',
        top: !show ? '-3000px' : verticalPosition === 'top' ? touchOffsetTop + 'px' : (triggerHeight - offset + touchMarginOffset + 2) + 'px',
        transform: `translate(0px, ${offset}px)`
      }
    },
    rippleStyle () {
      const {horizontalPosition, verticalPosition} = this
      return {
        left: horizontalPosition === 'center' ? '50%' : horizontalPosition === 'left' ? '100%' : '0%',
        top: verticalPosition === 'bottom' ? '0' : '100%'
      }
    }
  },
  methods: {
    setRippleSize () {
      const ripple = this.$refs.ripple
      const tooltip = this.$el
      const tooltipWidth = parseInt(tooltip.offsetWidth, 10) / (this.horizontalPosition === 'center' ? 2 : 1)
      const tooltipHeight = parseInt(tooltip.offsetHeight, 10)
      const rippleDiameter = Math.ceil((Math.sqrt(Math.pow(tooltipHeight, 2) + Math.pow(tooltipWidth, 2)) * 2))
      if (this.show) {
        ripple.style.height = `${rippleDiameter}px`
        ripple.style.width = `${rippleDiameter}px`
      } else {
        ripple.style.width = '0px'
        ripple.style.height = '0px'
      }
    },
    setTooltipSize () {
      this.offsetWidth = this.$el.offsetWidth
      if (!this.trigger) return
      this.triggerWidth = this.trigger.offsetWidth
      this.triggerHeight = this.trigger.offsetHeight
    }
  },
  mounted () {
    this.setRippleSize()
    this.setTooltipSize()
  },
  beforeUpdate () {
    this.setTooltipSize()
  },
  updated () {
    this.setRippleSize()
  }
}